package com.examination.client.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.examination.client.domian.dto.CourierSiteDTO;
import com.examination.client.domian.dto.CourierSiteQueryDTO;
import com.examination.client.domian.dto.SubsiteInDTO;
import com.examination.client.domian.dto.TreeCourierSiteQueryDTO;
import com.examination.client.domian.vo.CourierSiteTreeDTO;
import com.examination.client.entity.CourierInfo;
import com.examination.client.entity.CourierSite;
import com.examination.client.enums.DeleteStatusEnum;
import com.examination.client.enums.SiteStatusEnum;
import com.examination.client.mapper.CourierSiteMapper;
import com.examination.client.service.ICourierSiteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.examination.client.util.UserInfoUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.util.TextUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 快递站点信息表 服务实现类
 * </p>
 *
 * @author xiaodong
 * @since 2021-10-17
 */
@Slf4j
@Service
public class CourierSiteServiceImpl extends ServiceImpl<CourierSiteMapper, CourierSite> implements ICourierSiteService {

    @Override
    public List<CourierSite> list(CourierSiteQueryDTO courierSiteQueryDTO) {
        LambdaQueryWrapper<CourierSite> queryWrapper = new LambdaQueryWrapper<>();
        if(Objects.nonNull(courierSiteQueryDTO.getType()) && 1==courierSiteQueryDTO.getType()){
            queryWrapper.ne(CourierSite::getParentId,0)
                    .eq(CourierSite::getStatus, SiteStatusEnum.Enable.getCode())
                    .eq(CourierSite::getDeleteStatus, DeleteStatusEnum.NOT_DELETED.getCode());

        }else {
            if (Objects.nonNull(courierSiteQueryDTO.getUserType()) && 1==courierSiteQueryDTO.getUserType()) {
                String appSite = UserInfoUtil.getAppSite();
                log.info("appSite --> {}", appSite);
                List<Integer> appSiteList = new ArrayList<>();
                if (!TextUtils.isEmpty(appSite)) {
                    if (appSite.contains(",")) {
                        String[] list = appSite.split(",");
                        for (int i = 0; i < list.length; i ++) {
                            appSiteList.add(Integer.parseInt(list[i]));
                        }
                    } else {
                        appSiteList.add(Integer.parseInt(appSite));
                    }
                }
                queryWrapper.in(CourierSite::getId, appSiteList);
            }
            if(Objects.isNull(courierSiteQueryDTO.getSiteId())){
                courierSiteQueryDTO.setSiteId(UserInfoUtil.getSiteId());
            }
            queryWrapper.eq(CourierSite::getParentId, courierSiteQueryDTO.getSiteId())
                    .eq(CourierSite::getStatus, SiteStatusEnum.Enable.getCode())
                    .eq(CourierSite::getDeleteStatus, DeleteStatusEnum.NOT_DELETED.getCode());
        }
        return list(queryWrapper);
    }

    @Override
    public Boolean save(CourierSiteDTO courierSiteDTO) {
        CourierSite courierSite = new CourierSite();
        BeanUtils.copyProperties(courierSiteDTO,courierSite);
        courierSite.setCreateTime(new Date());
        return save(courierSite);
    }

    @Override
    public Boolean modify(CourierSiteDTO courierSiteDTO) {
        CourierSite courierSite = new CourierSite();
        BeanUtils.copyProperties(courierSiteDTO,courierSite);
        courierSite.setCreateTime(new Date());
        return updateById(courierSite);
    }

    @Override
    public Boolean modifySubsiteIn(SubsiteInDTO subsiteInDTO) {
        CourierSite courierSite = getById(subsiteInDTO.getId());
        courierSite.setSubsiteIn(subsiteInDTO.getSubsiteIn());
        courierSite.setUpdateTime(new Date());
        return updateById(courierSite);
    }

    @Override
    public List<CourierSiteTreeDTO> treeList(TreeCourierSiteQueryDTO treeCourierSiteQueryDTO) {
        LambdaQueryWrapper<CourierSite> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(treeCourierSiteQueryDTO.getSiteName()), CourierSite::getSiteName, treeCourierSiteQueryDTO.getSiteName());
        queryWrapper.eq(CourierSite::getDeleteStatus, DeleteStatusEnum.NOT_DELETED.getCode());
        List<CourierSite> courierSiteList = list(queryWrapper);
        if(CollectionUtils.isEmpty(courierSiteList)){
          return new ArrayList<>();
        }
        List<CourierSiteTreeDTO> courierSiteTreeDTOList = Lists.newArrayList();
        for (CourierSite courierSite:courierSiteList) {
            CourierSiteTreeDTO courierSiteTreeDTO = new CourierSiteTreeDTO();
            BeanUtils.copyProperties(courierSite,courierSiteTreeDTO);
            courierSiteTreeDTOList.add(courierSiteTreeDTO);
        }

        return buildTree(courierSiteTreeDTOList,0);
    }


    private static List<CourierSiteTreeDTO> buildTree(List<CourierSiteTreeDTO> deptList, int pid){
        List<CourierSiteTreeDTO> treeList = new ArrayList<>();
        for (CourierSiteTreeDTO dept : deptList) {
            if (dept.getParentId() == pid) {
                dept.setChildList(buildTree(deptList, dept.getId()));
                treeList.add(dept);
            }
        }
        return treeList;
    }
}
